package fu.capstone.hanoicityguide.presentation.fragment.food;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.food.FoodBO;
import fu.capstone.hanoicityguide.business.bo.food.FoodBOImp;
import fu.capstone.hanoicityguide.business.model.Food;
import fu.capstone.hanoicityguide.common.utility.InternetSetting;
import fu.capstone.hanoicityguide.presentation.adapter.ViewPagerAdapter;
import fu.capstone.hanoicityguide.presentation.fragment.home.HomeFragment;

@SuppressLint("ValidFragment")
public class DetailFoodFragment extends Fragment {

	private FoodBO foodBO;
	private int foodID;
	private ViewPager imgFragment;
	private ViewPagerAdapter vpagerAdapter;
	private TextView foodDescription;
	private TextView foodInformation;
	private Button btnWhereToTry;
	private InternetSetting internetSetting;
	// constructor
	public DetailFoodFragment() {

	}

	public DetailFoodFragment(int foodID) {
		this.foodID = foodID;
	}

	// getter and setter
	public int getFoodID() {
		return this.foodID;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View browseFoodFragment = null;
		internetSetting = new InternetSetting(container.getContext());
		try {
			browseFoodFragment = inflater.inflate(
					R.layout.fragment_detail_food, container, false);

			foodBO = new FoodBOImp(container.getContext());
			btnWhereToTry = (Button) browseFoodFragment.findViewById(R.id.btnWhereToTry);
			// get data from database
			Food food = foodBO.browseFood(foodID);

			// fill data into components
			foodDescription = (TextView) browseFoodFragment
					.findViewById(R.id.txtFoodDescription);
			foodInformation = (TextView) browseFoodFragment
					.findViewById(R.id.txtFoodInformation);
			foodDescription.setText(food.getDesc());
			
			// setup viewpager
			imgFragment = (ViewPager) browseFoodFragment
					.findViewById(R.id.foodPager);
			vpagerAdapter = new ViewPagerAdapter(getActivity(),
					food.getArrImage());
			imgFragment.setAdapter(vpagerAdapter);
			String information = "";

			if (!"".equals(String.valueOf(food.getPrice()).trim())) {
				information = information.concat("Price : About ");
				information = information.concat(food.getPrice() + " VNĐ");
				information = information.concat("\n");
			}
			if (!"".equals(food.getAddress().trim())) {
				information = information.concat("Address : ");
				information = information.concat(food.getAddress());
				information = information.concat("\n");
			}
			if (!"".equals(food.getPhoneNumber().trim())) {
				information = information.concat("Phone number : ");
				information = information.concat(food.getPhoneNumber());
				information = information.concat("\n");
			}
			foodInformation.setText(information);
			btnWhereToTry.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// button where to try
					Fragment fragment = new Fragment();
					if (!internetSetting.getConnectivityStatus(getActivity()) && !HomeFragment.isPurchased) {
						internetSetting.showSettingsAlert();
					}
					
					if (!internetSetting.getConnectivityStatus(getActivity()) && HomeFragment.isPurchased){
						fragment = new FoodOfflineMapFragment();
						Bundle bundle = new Bundle();
						bundle.putInt("FoodID", foodID);
						fragment.setArguments(bundle);
						if (fragment != null) {
							FragmentManager fragmentManager = getFragmentManager();
							FragmentTransaction fTransaction = fragmentManager
									.beginTransaction();
							fTransaction.addToBackStack(null);
							fTransaction.replace(R.id.frame_container, fragment)
									.commit();
							getActivity().setTitle("View on map");
						}
					}
					
					if (internetSetting.getConnectivityStatus(getActivity())) {
						fragment = new FoodOnlineMapFragment();
						Bundle bundle = new Bundle();
						bundle.putInt("FoodID", foodID);
						fragment.setArguments(bundle);
						if (fragment != null) {
							FragmentManager fragmentManager = getFragmentManager();
							FragmentTransaction fTransaction = fragmentManager
									.beginTransaction();
							fTransaction.addToBackStack(null);
							fTransaction.replace(R.id.frame_container, fragment)
									.commit();
							getActivity().setTitle("View on map");
						}
					}
				}
			});

		} catch (Exception e) {
			// TODO: handle exception
			Log.e("DetailFoodFragment : ", e.toString());
		}

		return browseFoodFragment;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getActivity().getActionBar().setTitle(
				foodBO.browseFood(foodID).getName());
	}
}
